import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class EcOperation:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://ecshop.test2.shopex123.com/')
        self.driver.maximize_window()

    def quit(self, seconds=5):
        time.sleep(seconds)
        self.driver.quit()

    # 搜索功能
    def search(self):
        info = input('请输入要搜索的商品:')
        self.driver.find_element(By.ID, 'keyword').send_keys(info)
        time.sleep(5)
        self.driver.find_element(By.CLASS_NAME, 'btn_search').click()
        time.sleep(3)

    # 注册功能
    def register(self, username, email, password, passwords):
        self.driver.find_element(By.LINK_TEXT, '[ 免费注册 ]').click()
        self.driver.find_element(By.ID, 'username').send_keys(username)
        self.driver.find_element(By.ID, 'email').send_keys(email)
        self.driver.find_element(By.ID, 'password1').send_keys(password)
        self.driver.find_element(By.ID, 'confirm_password').send_keys(passwords)
        time.sleep(10)
        self.driver.find_element(By.NAME, 'Submit').click()
        time.sleep(20)

    # 登录功能
    def login(self, username, password):
        self.driver.find_element(By.CLASS_NAME, 'sign').click()
        self.driver.find_element(By.XPATH, '//form[@name="formLogin"]/div/div/input[@placeholder="用户名"]').send_keys(
            username)
        self.driver.find_element(By.XPATH, '//form[@name="formLogin"]/div/div/input[@placeholder="密码"]').send_keys(
            password)
        self.driver.find_element(By.XPATH, '//input[@name="submit"]').click()
        time.sleep(5)

    # 退出账户功能(配合登录或者注册成功之后一起使用)
    def logout(self):
        self.driver.find_element(By.LINK_TEXT, '退出').click()
        time.sleep(5)


# 调用
if __name__ == '__main__':
    case = EcOperation()
    case.search()

    case.register('yangyu', '1130780712@qq.com', 'yy123456', 'yy123456')
    case.logout()

    case.login('yangyu', '123456')
    case.logout()

    case.quit()
